<?php

namespace App\Admin\Forms;


use App\Models\Posts\WxPost;
use App\Models\WxCircle;
use App\Models\WxNotice;
use App\Models\WxShopOrder;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;


class OrderForms extends Form implements LazyRenderable
{
    use LazyWidget;


    /**
     * 主题颜色.
     *
     * @var array
     */
    protected $status = [
        1 => '审核通过',
        2 => '驳回',
    ];

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        $id = $input['id'];
        $express = $input['express'];
        $express_no = $input['express_no'];
        $remark = $input['remark'];

        $ship_at = date('Y-m-d H:i:s', time());
        WxShopOrder::where('id', $id)->update([
            'express' => $express,
            'express_no' => $express_no,
            'remark' => $remark,
            'ship_at' => $ship_at,
            'status' => 2,
        ]);

        $oinfo = WxShopOrder::where('id', $id)->first();

        $nr = '您的订单：' . $oinfo['order_id'] . ' 由：' . $express . '：' . $express_no . ' 已经发出了。';

        // 发送通知
        (new WxNotice())->addNotice($oinfo['user_id'], '商品发货通知', $nr, 8, '', $id);

        return $this->response()
            ->success('发货成功')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->hidden('id')->value($this->payload["id"]);

        $this->text('express', "物流名称");
        $this->text('express_no', "物流单号");
        $this->textarea('remark', "备注");
    }

}
